<?php

namespace app\admin\controller\booking;

use app\common\controller\Backend;

/**
 * Ajax
 *
 * @icon fa fa-circle-o
 * @internal
 */
class Base extends Backend
{

    protected $authHouseIds = null;
    protected $authStoreIds = null;
    protected $addonsConfig = null;

    protected $dataLimit = 'auth';

    public function _initialize()
    {
        parent::_initialize();

        $this->addonsConfig = get_addon_config('booking');

        $this->dataLimit = $this->addonsConfig['authModel'];

        if ($this->addonsConfig['isStoreAllocate']==1) {

            $adminIds = parent::getDataLimitAdminIds();

            $storeAdminList = (new \app\admin\model\booking\StoreAdmin())
                ->field("sa.id,sa.store_ids,GROUP_CONCAT(h.id) house_ids")
                ->alias('sa')
                ->join('booking_house h', "FIND_IN_SET(h.store_id,sa.store_ids)", 'LEFT')
                ->where('admin_id', 'in', $adminIds)
                ->group('sa.id')
                ->select();

            $storeIds = [];
            $houseIds = [];

            foreach ($storeAdminList as $res) {
                $storeIds = array_merge($storeIds, explode(',', $res['store_ids']));
                $houseIds = array_merge($houseIds, explode(',', $res['house_ids']));
            }

            $this->authStoreIds = array_filter(array_unique($storeIds));
            $this->authHouseIds = array_filter(array_unique($houseIds));
        }
    }
}
